﻿#region Usings

using System.Collections.Specialized;
using System.Text;
using System.Web;

#endregion

namespace Kazkommertsbank.Framework.Common
{
    public class RemoteForm
    {
        public RemoteForm()
        {
            Fields = new NameValueCollection();
        }

        public RemoteForm(string formName, string postURL)
        {
            FormName = formName;
            Url = postURL;
            Fields = new NameValueCollection();
        }

        public RemoteForm(string formName, string postURL, NameValueCollection fields)
        {
            FormName = formName;
            Url = postURL;
            Fields = fields;
        }

        public NameValueCollection Fields { get; set; }

        public string FormName { get; set; }

        public string Url { get; set; }

        public void Add(string fieldName, string value)
        {
            Fields.Add(fieldName, value);
        }

        public void Post()
        {
            Post(true, null, null);
        }

        public void Post(bool hidden, string caption, string imgSrc)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("<html><head></head><body onload=\"document.{0}.submit()\">", FormName);
            builder.AppendFormat("<form name=\"{0}\" method=\"POST\" action=\"{1}\" >", FormName, Url);
            if(!string.IsNullOrEmpty(caption))
                builder.AppendFormat("<h1 style=\"font:14px Verdana, Arial, Helvetica, sans-serif;font-weight:900;margin:auto;width:100%;\">{0}", caption);
            if(!string.IsNullOrEmpty(imgSrc))
                builder.AppendFormat("<img src=\"{0}\" alt=\"\"></img>", imgSrc);
            if(!string.IsNullOrEmpty(caption))
                builder.Append("</h1>");
            foreach(string str in Fields)
                builder.AppendFormat(hidden ? "<input type=\"hidden\" name=\"{0}\" value=\"{1}\" \\>" : "<input name=\"{0}\" value=\"{1}\" \\>",
                                     str,
                                     Fields[str]);
            builder.Append("</form></body></html>");
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(builder.ToString());
            //HttpContext.Current.Response.End();
        }
    }
}